var app = getApp();
var api = require('../../api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex: 0,
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
  },

  /*这里实现控制中间凸显图片的样式 */
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var page = this;
    app.request({
      url: api.teacher.list,
      data: {

      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            list: res.data
          });
        }
      }
    });
    
    if (options.uid) {
      var uid = options.uid;
      if (uid) {
        page.agent(uid);
      }
    }
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var list = this.data.list;
    if (list){
      var user_info = wx.getStorageSync('user_info');
      var teacher = list[0];
      var result = {
        title: teacher.name,
        path: "/pages/teacher-info/teacher-info?pid=" + user_info.user_id,
        imageUrl: teacher.icon,
        success: function (res) {
          
        },
        fail: function (res) {
          // 转发失败
        }
      };
      return result;
    }

  },

  teacher: function (options){
    var page = this;
    var index = options.currentTarget.dataset.index;
    var list=this.data.list;
    var teacher = list[index];
    wx.navigateTo({
      url: '/pages/teacher-detail/teacher-detail?id=' + teacher.id,
    })
  },

  agent: function (uid) {
    app.request({
      url: api.distribution.agent,
      data: {
        puid: uid
      },
      success: function (res) {
        if (res.code == 0) {

        }
      },
      complete: function () {

      }
    });
  },
})